import subprocess
def create(arr = [["告警配置表","A",'''名称
告警目标类型
告警级别
关联场景名称
关联场景Id
状态
场景触发类型
说明
创建者ID(只读)
创建时间(只读)
更新者ID
更新时间
id''']],relation = [["属于","B","1","N","告警处理记录-","告警配置表-"]]):
    line1 = [];
    line2 = [];
    line3 = [];
    line4 = [];
    for i in range(0, len(arr)):
        x = arr[i]
        title = x[0]
        titleL = x[1]+f"{i}mm"
        attrs = x[2].split("\n")
        line1.insert(0,f'  {titleL}  [label="{title}"];\n')
        line2.insert(0,'\n'.join([f'  {titleL}{j} [label="{attrs[j]}",style=filled, fillcolor="#eceefd"];\n' for j in range(0, len(attrs))]))
        line3.insert(0,'\n'.join([f'  {titleL} -- {titleL}{j} [len=2];\n' for j in range(0, len(attrs))]))


    for i in range(0, len(relation)):
        x = relation[i]
        title = x[0]
        titleL = x[1]+f"{i}mm"
        lineLeft = x[2]
        lineRight = x[3]
        arrVarLeftIndex = [z[0] for z in arr].index(x[4]);
        varLeft = arr[arrVarLeftIndex][1]+f"{arrVarLeftIndex}mm"
        arrVarRightIndex = [z[0] for z in arr].index(x[5]);
        varRight = arr[arrVarRightIndex][1]+f"{arrVarRightIndex}mm"
        line4.append(f'  {titleL} [shape=diamond, label="{title}",style=filled, fillcolor="#f9e3b6"];\n')
        line4.append(f'  {varLeft} -- {titleL}[label="{lineLeft}",len=2];\n')
        line4.append(f'  {titleL} -- {varRight}[label="{lineRight}",len=2];\n')




    with open("input.dot", 'w+',encoding='utf-8') as f:
        f.write('''graph {
  // 设置节点形状为矩形
  node [shape=box, style=filled, fillcolor="#ced7fd"];

  // 创建中间的A节点
  '''+"".join(line1)+'''
  // 设置节点形状为椭圆形，底色为#eceefd
  node [shape=ellipse, style=filled, fillcolor="#eceefd"];

  // 创建周围的BCDEF节点，底色为#eceefd
  
  '''+"".join(line2)+'''

  // 设置边的颜色为#fdfdfd
  // edge [color="#fdfdfd"];
  layout=neato;
  // 创建节点之间的连接
  '''+"".join(line3)+'''
'''+"".join(line4)+'''
}
    ''')
def doall(arr2):
    with open("/Users/wangshuguan/Desktop/filltext2.txt", 'r',encoding='utf-8') as f:
        arr = [[x.split('\n')[0],'A','\n'.join(x.split('\n')[1:])] for x in f.read().split("\n\n")]
        create(arr,arr2)
        subprocess.call("dot -Tpng input.dot -o output.png",shell=True)

def dotest():
    with open("/Users/wangshuguan/Desktop/filtedtext.txt", 'r',encoding='utf-8') as f:
        arr = [[x.split('\n')[0],'A','\n'.join(x.split('\n')[1:])] for x in f.read().split("\n\n")[:2]]
        create(arr)
        subprocess.call("dot -Tpng input.dot -o output.png",shell=True)
doall([
    ["出售","B","1","N","用户表-","房屋出售表-"],
    ["出租","B","1","N","用户表-","房屋出租表-"],
    ["洗车","B","1","N","用户表-","洗车表-"],
    ["邀请","B","1","N","用户表-","全民经纪人-"],
    ["家电清洗","B","1","N","用户表-","家电清洗商品-"],
    ["保洁","B","1","N","用户表-","家政保洁-"],
    ["家电维修","B","1","N","用户表-","家电维修-"],
    ["安装","B","1","N","用户表-","家电安装-"],
    ["缴费","B","1","N","用户表-","代缴费用-"],
    ["详情","B","1","1","用户表-","个人信息-"],
    ["所有","B","1","N","用户表-","我的房屋-"],
    ["所有","B","1","N","用户表-","我的车辆-"],
    ["所有","B","1","N","用户表-","家庭成员-"],
    ["查看","B","N","N","用户表-","投票表决-"],
    ["参加","B","N","N","用户表-","问卷调查-"],
    ["拥有","B","N","N","用户表-","我的订单-"],
    ["服务","B","N","N","用户表-","家政订单-"],
        ["所属","B","N","N","用户表-","1`````````"],
    ["属于","B","N","1","家电清洗商品-","清洗类别-"],
])